package com.xpd.spring.demo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {
    @Before("execution(* com.xpd.spring.demo..*.find*(..))")
    public void log(JoinPoint joinPoint){
        //获得目标方法的签名  目标方法的详细信息
        Signature signature = joinPoint.getSignature();
/*
        joinPoint   连接点
*/

        System.out.println("前置增强");
    }
    //切点方法
    @Pointcut("execution(* com.xpd.spring.demo.web.*.find*(..))")
    public void pointcut(){
    }


    @After("pointcut()")
    public void after(){
        System.out.println("后置增强");
    }
    @AfterReturning("pointcut()")
    public void AfterReturning(){
        System.out.println("返回增强");
    }
    @AfterThrowing("pointcut()")
    public void AfterThrowing(){
        System.out.println("异常增强");
    }

    //环绕增强
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        //proceedingJoinPoint  提供了执行业务方法的方法
        //业务方法由增强方法执行
        //增强方法的返回值就是业务方法的返回值
        long time1 = System.currentTimeMillis();
        long time2=0;
        try{

            Object proceed = proceedingJoinPoint.proceed();
            time2=System.currentTimeMillis();
           return proceed;

        }catch (Throwable e){
            time2=System.currentTimeMillis();
            throw new RuntimeException(e);
        }finally {
            System.out.println("环绕增强共耗时"+((time2-time1)));
        }
    }


    //登陆成功的前置增强
    @Before("execution(void com.xpd.spring.demo.biz..login(..))")
    public void login(){
        System.out.println("登陆成功的前置增强");
    }
}
